package com.design.YiTong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.YiTong.model.medicinal.MedicinalShop;
import com.design.YiTong.model.user.DocInfo;
import com.design.YiTong.user.mapper.DoctorInfoMapper;
import com.design.YiTong.user.service.DocInfoService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;

@Service
public class DocInfoServiceImpl extends
        ServiceImpl<DoctorInfoMapper, DocInfo>implements DocInfoService {

    @Override
    public List<DocInfo> showDoc( String docDep) {
        QueryWrapper<DocInfo>wrapper=new QueryWrapper<>();
        wrapper.eq("docDep",docDep);
        wrapper.eq("status",0);
        List<DocInfo> pages = baseMapper.selectList(wrapper);
        for(int i=0;i<pages.size();i++){
            String url="";
            String image="";
            if(pages.get(i).getImage()!=null&&!pages.get(i).getImage().equals("")) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(pages.get(i).getImage()));
                    while (true) {
                        url = br.readLine();//readLine()读入一行后会自动换行
                        if (url==null) break;
                        image+=url;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            pages.get(i).setImage(image);
        }
        return pages;
    }
    @Override
    public List<DocInfo> showAuth() {
        QueryWrapper<DocInfo>wrapper=new QueryWrapper<>();
        List<DocInfo> pages = baseMapper.selectList(wrapper);
        for(int i=0;i<pages.size();i++){
            String url="";
            String image="";
            if(pages.get(i).getImage()!=null&&!pages.get(i).getImage().equals("")) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(pages.get(i).getImage()));
                    while (true) {
                        url = br.readLine();//readLine()读入一行后会自动换行
                        if (url==null) break;
                        image+=url;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            pages.get(i).setImage(image);
        }
        return pages;
    }
    @Override
    public DocInfo searchById(long id){
        DocInfo docInfo=baseMapper.selectById(id);
        return docInfo;
    }
    @Override
    public void DocAuth(DocInfo docInfo) {
        DocInfo Info=searchById(docInfo.getId());
        File file=new File(Info.getId().toString());
        if(Info==null){
            if(docInfo.getImage()!=null) {
                try {
                    file.createNewFile();
                    FileWriter write = new FileWriter("Doc"+Info.getId().toString());
                    BufferedWriter bw = new BufferedWriter(write);
                    bw.write(docInfo.getImage());
                    bw.close();
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            docInfo.setImage("Doc"+Info.getId().toString());
            baseMapper.insert(docInfo);
        }
        if(docInfo.getImage()!=null) {
            try {
                FileWriter write = new FileWriter("Doc"+Info.getId().toString());
                BufferedWriter bw = new BufferedWriter(write);
                bw.write(docInfo.getImage());
                bw.close();
                write.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        docInfo.setImage("Doc"+Info.getId().toString());
        baseMapper.updateById(docInfo);
    }

    @Override
    public void access(long id) {
        QueryWrapper<DocInfo>wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        DocInfo docInfo=baseMapper.selectOne(wrapper);
        if(docInfo.getStatus()==1) docInfo.setStatus(0);
        else docInfo.setStatus(1);
        baseMapper.updateById(docInfo);
    }

}
